package net.lzzy.practices.web.handler;


import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

/**
 * @author  Jaymo on 2022/5/6.
 * Description:
 */
@RestControllerAdvice(annotations = RestController.class)
public class ApiExceptionHandler {

    public static  class ExceptionWrapper{
        private  String desc;
        private String message;
        private  StackTraceElement [] elements;
        public  ExceptionWrapper(String desc,String message, StackTraceElement[] elements) {
            this.desc = desc;
            this.message = message;
            this.elements = elements;
        }
    }

    @ExceptionHandler(Exception.class)
    public ExceptionWrapper handler(WebRequest request, Exception e) {
        return new ExceptionWrapper(request.getDescription(true), e.getMessage(), e.getStackTrace());
    }
}
